package com.nowcoder.community.config;

import com.nowcoder.community.quartz.AlphaJob;
import com.nowcoder.community.quartz.PostScoreRefrushJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {

    //BeanFactory是生成bean的工厂，常说的spring容器
    /*
      FactoryBean是一种特殊的bean，这种bean可简化实例化过程。
      1.通过FactoryBean封装了bean的实例化过程
      2.将FactoryBean装到spring容器中
      3.将FactoryBean注入给其他的bean，那么这个bean得到的就是FactoryBean所管理对象的实例
    */

    //创建JobDetail和Trigger的FactoryBean

    //配置JobDetail（Job详情）
    /*@Bean
    public JobDetailFactoryBean alphaJobDetail(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");
        factoryBean.setGroup("alphaJobGroup");
        factoryBean.setDurability(true);
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }*/

    //配置Trigger（触发器）
    /*@Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail){
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTriggerGroup");
        factoryBean.setRepeatInterval(3000); //每个三秒触发一次
        factoryBean.setJobDataMap(new JobDataMap());//存储Job状态
        return factoryBean;
    }*/


    @Bean
    public JobDetailFactoryBean postScoreRefrushJobDetail(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefrushJob.class);//作业为PostScoreRefrushJob
        factoryBean.setName("postScoreRefrushJob");//作业名称
        factoryBean.setGroup("communityJobGroup");//作业组
        factoryBean.setDurability(true);//设置将作业持久化
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    @Bean
    public SimpleTriggerFactoryBean postScoreRefrushTrigger(JobDetail postScoreRefrushJobDetail){
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefrushJobDetail);
        factoryBean.setName("postScoreRefrushTrigger");//触发器名称
        factoryBean.setGroup("communityTriggerGroup");//触发器组
        factoryBean.setRepeatInterval(1000 * 60 * 5);//每隔5分钟触发一次
        factoryBean.setJobDataMap(new JobDataMap());//存储Job状态
        return factoryBean;
    }
}
